<template>
  <div>
    <div class="gva-form-box">
      <el-form :model="formData" ref="elFormRef" label-position="right" :rules="rule" label-width="80px">
        <el-form-item label="平台类型:" prop="platId">
          <el-select v-model="formData.platId" placeholder="请选择" :clearable="false">
            <el-option v-for="(item,key) in platOptions" :key="key" :label="item.label" :value="item.value" />
          </el-select>
       </el-form-item>
        <el-form-item label="包名:" prop="appId">
          <el-input v-model="formData.appId" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="渠道名:" prop="channelId">
          <el-input v-model="formData.channelId" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="排序:" prop="orderNum">
          <el-input v-model.number="formData.orderNum" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="产品标识:" prop="productId">
          <el-input v-model="formData.productId" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="产品名称:" prop="productName">
          <el-input v-model="formData.productName" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="档位描述:" prop="description">
          <el-input v-model="formData.description" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="档位底色:" prop="backgroundColor">
          <el-select v-model="formData.backgroundColor" placeholder="请选择" :clearable="true">
            <el-option v-for="(item,key) in productBackgroundColorOptions" :key="key" :label="item.label" :value="item.value" />
          </el-select>
       </el-form-item>
        <el-form-item label="原价:" prop="originCost">
          <el-input v-model.number="formData.originCost" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="实际价格（分）:" prop="cost">
          <el-input v-model.number="formData.cost" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="看点（个）:" prop="amount">
          <el-input v-model.number="formData.amount" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="赠送看点（个）:" prop="give">
          <el-input v-model.number="formData.give" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="角标描述:" prop="logoDesc">
          <el-input v-model="formData.logoDesc" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="角标样式:" prop="logoStyle2">
          <el-select v-model="formData.logoStyle2" placeholder="请选择" :clearable="true">
            <el-option v-for="(item,key) in productLogoStyleOptions" :key="key" :label="item.label" :value="item.value" />
          </el-select>
       </el-form-item>
        <el-form-item label="档位场景类型：1通用, 2定制短剧档位库:" prop="mode">
          <el-input v-model.number="formData.mode" :clearable="false" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="类型:" prop="type">
          <el-select v-model="formData.type" placeholder="请选择" :clearable="false">
            <el-option v-for="(item,key) in productTypeOptions" :key="key" :label="item.label" :value="item.value" />
          </el-select>
       </el-form-item>
        <el-form-item label="状态:" prop="status">
          <el-select v-model="formData.status" placeholder="请选择" :clearable="false">
            <el-option v-for="(item,key) in productStatusOptions" :key="key" :label="item.label" :value="item.value" />
          </el-select>
       </el-form-item>
        <el-form-item label="创建人:" prop="createUid">
          <el-input v-model.number="formData.createUid" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="创建时间:" prop="createTime">
          <el-date-picker v-model="formData.createTime" type="date" placeholder="选择日期" :clearable="true"></el-date-picker>
       </el-form-item>
        <el-form-item label="更新人:" prop="updateUid">
          <el-input v-model.number="formData.updateUid" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="更新时间:" prop="updateTime">
          <el-date-picker v-model="formData.updateTime" type="date" placeholder="选择日期" :clearable="true"></el-date-picker>
       </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {
  createTProduct,
  updateTProduct,
  findTProduct
} from '@/api/tProduct'

defineOptions({
    name: 'TProductForm'
})

// 自动获取字典
import { getDictFunc } from '@/utils/format'
import { useRoute, useRouter } from "vue-router"
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'

const route = useRoute()
const router = useRouter()

const type = ref('')
const platOptions = ref([])
const productBackgroundColorOptions = ref([])
const productLogoStyleOptions = ref([])
const productTypeOptions = ref([])
const productStatusOptions = ref([])
const formData = ref({
            platId: undefined,
            appId: '',
            channelId: '',
            orderNum: 0,
            productId: '',
            productName: '',
            description: '',
            backgroundColor: undefined,
            originCost: 0,
            cost: 0,
            amount: 0,
            give: 0,
            logoDesc: '',
            logoStyle2: undefined,
            mode: 0,
            type: undefined,
            status: undefined,
            createUid: 0,
            createTime: new Date(),
            updateUid: 0,
            updateTime: new Date(),
        })
// 验证规则
const rule = reactive({
               platId : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               appId : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               channelId : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               orderNum : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               productId : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               productName : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               originCost : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               cost : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               amount : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               type : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               status : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
})

const elFormRef = ref()

// 初始化方法
const init = async () => {
 // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
    if (route.query.id) {
      const res = await findTProduct({ ID: route.query.id })
      if (res.code === 0) {
        formData.value = res.data.retProduct
        type.value = 'update'
      }
    } else {
      type.value = 'create'
    }
    platOptions.value = await getDictFunc('plat')
    productBackgroundColorOptions.value = await getDictFunc('productBackgroundColor')
    productLogoStyleOptions.value = await getDictFunc('productLogoStyle')
    productTypeOptions.value = await getDictFunc('productType')
    productStatusOptions.value = await getDictFunc('productStatus')
}

init()
// 保存按钮
const save = async() => {
      elFormRef.value?.validate( async (valid) => {
         if (!valid) return
            let res
           switch (type.value) {
             case 'create':
               res = await createTProduct(formData.value)
               break
             case 'update':
               res = await updateTProduct(formData.value)
               break
             default:
               res = await createTProduct(formData.value)
               break
           }
           if (res.code === 0) {
             ElMessage({
               type: 'success',
               message: '创建/更改成功'
             })
           }
       })
}

// 返回按钮
const back = () => {
    router.go(-1)
}

</script>

<style>
</style>
